package com.hmyd.hmyd_android.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SlidingDrawer;

public class MySlidingDrawer extends SlidingDrawer{

	private boolean mVertical;  
    private int mTopOffset;
	
    public MySlidingDrawer(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);  
        mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);  
        mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);  
    }  
  
    public MySlidingDrawer(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);  
        mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);  
        mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);  
    }  
  
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);  
        int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);  
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);  
        int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);  
  
        final View handle = getHandle();  
        final View content = getContent();  
        measureChild(handle, widthMeasureSpec, heightMeasureSpec);  
  
        if (mVertical) {  
            int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;  
            content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode));  
            heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();  
            widthSpecSize = content.getMeasuredWidth();  
            if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();  
        }  
        else {  
            int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;  
            getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);  
            widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();  
            heightSpecSize = content.getMeasuredHeight();  
            if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();  
        }  
  
        setMeasuredDimension(widthSpecSize, heightSpecSize);  
    }

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {// 改变handle的位置
		super.onLayout(changed, l, t, r, b);
		final int height = b - t;

		final View handle = this.getHandle();

		int childWidth = handle.getMeasuredWidth();
		int childHeight = handle.getMeasuredHeight();

		int childLeft = 0;
		int childTop = 0;

//		childLeft = l + 20;//设置左边距

		// 想靠右的话
		 childLeft = childLeft + r - childWidth;

		childTop = this.isOpened() ? this.getPaddingTop() : height
				- childHeight + this.getPaddingBottom();

		handle.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
	}
	
	/*@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (mTracking) {
            return;
        }

        final int width = r - l;
        final int height = b - t;

        final View handle = mHandle;

        int childWidth = handle.getMeasuredWidth();
        int childHeight = handle.getMeasuredHeight();

        int childLeft;
        int childTop;

        final View content = mContent;

        if (mVertical) {
            childLeft = (width - childWidth) / 2;
            childTop = mExpanded ? mTopOffset : height - childHeight + mBottomOffset;

            content.layout(0, mTopOffset + childHeight, content.getMeasuredWidth(),
                    mTopOffset + childHeight + content.getMeasuredHeight());
        } else {
            childLeft = mExpanded ? mTopOffset : width - childWidth + mBottomOffset;
            childTop = (height - childHeight) / 2;

            content.layout(mTopOffset + childWidth, 0,
                    mTopOffset + childWidth + content.getMeasuredWidth(),
                    content.getMeasuredHeight());            
        }

        handle.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        mHandleHeight = handle.getHeight();
        mHandleWidth = handle.getWidth();
    }*/

}
